package sy.services.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.db.mysql.dao.TresourceDao;
import sy.db.mysql.dao.TroleDao;
import sy.db.mysql.dao.TroleTresourceDao;
import sy.db.mysql.model.Tresource;
import sy.db.mysql.model.TroleTresource;
import sy.pageModel.DataGrid;
import sy.pageModel.SessionInfo;
import sy.pageModel.Tree;
import sy.services.ResourceServiceI;

@Service
public class ResourceServiceImpl implements ResourceServiceI {

	@Autowired
	TresourceDao tresourceDao;
	@Autowired
	TroleDao troleDao;
	@Autowired
	TroleTresourceDao troleTresourceDao;
	@Override
	public List<Tree> tree(SessionInfo sessionInfo ,int deep) {
		List<Tree> lt = new ArrayList<Tree>();

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("TRESOURCETYPEID_EQ", "0");// 菜单类型的资源
		params.put("PIDSTR_EQ", "root");
		params.put("sort", "SEQNUM");
		params.put("order", "DESC");
		if (sessionInfo != null) {
			params.put("TUSERIDSTR_EQ","0");// 自查自己有权限的资源
		}
		List<Map> l = tresourceDao.findResourceByUserIDByGrid(params);
		if (l != null && l.size() > 0) {
			for (int i=l.size()-1 ;i>=0 ;i--) {
				Map r = l.get(i);
				Tree tree = new Tree();
				tree.setId(r.get("ID").toString());
				tree.setText(r.get("NAMESTR").toString());
				tree.setIconCls(r.get("ICONSTR").toString());
				Map<String, Object> attr = new HashMap<String, Object>();
				attr.put("url", r.get("URLSTR"));
				tree.setAttributes(attr);
				Map<String, Object> params2 = new HashMap<String, Object>();
				params2.put("PIDSTR_EQ", r.get("ID"));// 菜单类型的资源
				params2.put("TRESOURCETYPEID_EQ", "0");// 菜单类型的资源
				params2.put("sort", "SEQNUM");
				params2.put("order", "ASC");
				if (sessionInfo != null) {
					params2.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
				}
				List<Map> l2 = tresourceDao.findResourceByUserIDByGrid(params2);
				if(l2==null || l2.size() ==0){
					continue;
				}
				List<Tree> children = new ArrayList<Tree>();
				tree.setChildren(children);
				if(l2!=null && l2.size()>0){
					for(Map rr:l2){
						Tree child = new Tree();
						child.setId(rr.get("ID").toString());
						child.setText(rr.get("NAMESTR").toString());
						child.setIconCls(rr.get("ICONSTR").toString());
						Map<String, Object> attr2 = new HashMap<String, Object>();
						attr2.put("url", rr.get("URLSTR"));
						child.setAttributes(attr2);
						children.add(child);
						if(deep ==3 ){
							Map<String, Object> params3 = new HashMap<String, Object>();
							params3.put("PIDSTR_EQ", rr.get("ID"));// 菜单类型的资源
							params3.put("TRESOURCETYPEID_EQ", "1");// 菜单类型的资源
							params3.put("sort", "SEQNUM");
							params3.put("order", "ASC");
							if (sessionInfo != null) {
								params2.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
							}
							List<Map> l3 = tresourceDao.findResourceByUser2IDByGrid(params3);
							List<Tree> children2 = new ArrayList<Tree>();
							child.setChildren(children2);
							for(Map rrr:l3){
								try {
									Tree child2 = new Tree();
									child2.setId(rrr.get("ID").toString());
									child2.setText(rrr.get("NAMESTR").toString());
									child2.setIconCls(rrr.get("ICONSTR").toString());
									Map<String, Object> attr22= new HashMap<String, Object>();
									attr22.put("url", rrr.get("URLSTR"));
									child2.setAttributes(attr22);
									children2.add(child2);
								} catch (Exception e) {
								}
							}
						}
					}
				}
				lt.add(tree);
			}
		}
		return lt;
	}

	@Override
	public List<Tree> allTree(SessionInfo sessionInfo) {
		sessionInfo = new SessionInfo();
		sessionInfo.setId("0");
		return this.tree(sessionInfo ,3);
	}

	@Override
	public List<Map> treeGrid(SessionInfo sessionInfo) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("PIDSTR_EQ", "root");
		if (sessionInfo != null) {
			params.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
		}
		List<Map> root = tresourceDao.findResourceByUserIDByGrid(params);
		for(Map map1 :root){
			Map<String, Object> params2 = new HashMap<String, Object>();
			params2.put("PIDSTR_EQ", map1.get("ID").toString());
			if (sessionInfo != null) {
				params2.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
			}
			List<Map> menus = tresourceDao.findResourceByUserIDByGrid(params2);
			map1.put("children",menus);
			for(Map menu : menus){
				Map<String, Object> params3 = new HashMap<String, Object>();
				params3.put("PIDSTR_EQ", menu.get("ID").toString());
				if (sessionInfo != null) {
					params3.put("TUSERIDSTR_EQ", sessionInfo.getId());// 自查自己有权限的资源
				}
				List<Map> opers = tresourceDao.findResourceByUserIDByGrid(params3);
				menu.put("children",opers);
			}
		}
		return  root;
	}

	@Override
	public void add(Tresource resource, SessionInfo sessionInfo) {
		tresourceDao.insertSelective(resource);
		// 由于当前用户所属的角色，没有访问新添加的资源权限，所以在新添加资源的时候，将当前资源授权给当前用户的所有角色，以便添加资源后在资源列表中能够找到
		Map<String, String> params = new HashMap<String, String>();
		if (sessionInfo != null) {
			params.put("TUSERIDSTR_EQ", sessionInfo.getId());
		}
		List<Map> l = this.troleDao.findRoleByUserIDByGrid(params);
		for(Map role : l){
			try {
				TroleTresource troleTresource = new TroleTresource();
				troleTresource.setID(UUID.randomUUID().toString());
				troleTresource.setTROLEIDSTR(role.get("ID").toString());
				troleTresource.setTRESOURCEIDSTR(resource.getID());
				troleTresourceDao.insertSelective(troleTresource);
			} catch (Exception e) {
			}
		}
	}

	@Override
	public void delete(String id) {
		this.tresourceDao.deleteByPrimaryKey(id);
	}

	@Override
	public void edit(Tresource resource) {
		this.tresourceDao.updateByPrimaryKeySelective(resource);
		
	}

	@Override
	public Tresource get(String id) {
		return this.tresourceDao.selectByPrimaryKey(id);
	}

	@Override
	public DataGrid findbyByGrid(Map map) {
		DataGrid dg = new DataGrid();
		map.put("dataGrid", dg);
		dg.setRows(tresourceDao.findbyByGrid(map));
		return dg;
	}}
